package com.nulldev.util.analytics.v2;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.settings.Preferences;

public class nAnalytics2Manager {

	private static final long authToken = MathUtil.randLong(1000000000, Long.MAX_VALUE);
	private static final Preferences SETTINGS = new Preferences(
			FileUtil.safeCreate(
					Preferences.HOME_DIRECTORY + "/nanalytics2-settings.cfg"));
	
	public static boolean canReadCPU() {
		if (!SETTINGS.isKey("data.cpuAllowed")) {
			SETTINGS.writeOption("data.cpuAllowed", false);
		}
		return SETTINGS.readOptionBoolean("data.cpuAllowed");
	}
	
	public static boolean canReadGPU() {
		if (!SETTINGS.isKey("data.gpuAllowed")) {
			SETTINGS.writeOption("data.gpuAllowed", false);
		}
		return SETTINGS.readOptionBoolean("data.gpuAllowed");
	}
	
	public static boolean canAccessNetwork() {
		if (!SETTINGS.isKey("data.networkAllowed")) {
			SETTINGS.writeOption("data.networkAllowed", false);
		}
		return SETTINGS.readOptionBoolean("data.networkAllowed");
	}
	
	public static boolean canReadBIOS() {
		if (!SETTINGS.isKey("data.BIOSAllowed")) {
			SETTINGS.writeOption("data.BIOSAllowed", false);
		}
		return SETTINGS.readOptionBoolean("data.BIOSAllowed");
	}

	public static boolean canReadOS() {
		if (!SETTINGS.isKey("data.OSAllowed")) {
			SETTINGS.writeOption("data.OSAllowed", false);
		}
		return SETTINGS.readOptionBoolean("data.OSAllowed");
	}

	public static void writeOS(final boolean selected, final long authToken) {
		if (authToken != nAnalytics2Manager.authToken) {
			throw new RuntimeException("Invalid authToken provided!");
		}
		SETTINGS.writeOption("data.OSAllowed", selected);
	}
	
	public static void writeCPU(final boolean selected, final long authToken) {
		if (authToken != nAnalytics2Manager.authToken) {
			throw new RuntimeException("Invalid authToken provided!");
		}
		SETTINGS.writeOption("data.cpuAllowed", selected);
	}
	
	public static void writeGPU(final boolean selected, final long authToken) {
		if (authToken != nAnalytics2Manager.authToken) {
			throw new RuntimeException("Invalid authToken provided!");
		}
		SETTINGS.writeOption("data.gpuAllowed", selected);
	}
	
	public static void writeNetwork(final boolean selected, final long authToken) {
		if (authToken != nAnalytics2Manager.authToken) {
			throw new RuntimeException("Invalid authToken provided!");
		}
		SETTINGS.writeOption("data.networkAllowed", selected);
	}
	
	public static void writeBIOS(final boolean selected, final long authToken) {
		if (authToken != nAnalytics2Manager.authToken) {
			throw new RuntimeException("Invalid authToken provided!");
		}
		SETTINGS.writeOption("data.BIOSAllowed", selected);
	}
}
